package com.xw.service.imp;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.RandomUtil;
import com.github.pagehelper.PageHelper;
import com.xw.dao.UserConcernMapper;
import com.xw.dao.VideosMapper;
import com.xw.model.SysUserConcern;
import com.xw.model.Videos;
import com.xw.service.TeacherVo;
import com.xw.service.UserConcernService;
import com.xw.util.JsonData;
import com.xw.vo.FansVo;
import com.xw.vo.UserConcernVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @需求描述:
 * @创建人:whh
 * @创建时间: 2019/11/11
 * @修改需求:
 * @修改人:
 * @修改时间:
 * @需求思路:
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UserConcernServiceImpl implements UserConcernService {
    @Resource
    private UserConcernMapper userConcernMapper;
    @Resource
    private VideosMapper videosMapper;

    @Override
    public Map findMyFansByUserId(String userId, Integer page, Integer limit) {
        //我的粉丝里边包括导师和普通用户，对导师可以进行关注或者取消关注，对普通用户没有任何操作，点击导师可以进入导师详情页
        if (page != null && limit != null){
            PageHelper.startPage(page,limit,false);
        }
        List<FansVo> list = userConcernMapper.findMyFansByUserId(userId);
        return JsonData.success(list);
    }

    @Override
    public Map cancelFocusTeacherMP(String userId, String teacherId) {
        SysUserConcern model = new SysUserConcern();
        model.setUserId(userId);
        model.setTeacherId(teacherId);
        int delete = userConcernMapper.delete(model);
        return JsonData.returnMap(delete);
    }

    @Override
    public Map focusTeacherMP(String userId, String teacherId) {
        SysUserConcern model = new SysUserConcern();
        model.setUserId(userId);
        model.setTeacherId(teacherId);
        SysUserConcern userConcern = userConcernMapper.selectOne(model);
        if (userConcern == null){
            model.setConcernId(RandomUtil.simpleUUID());
            model.setCreateTime(new Date());
            userConcernMapper.insertSelective(model);
        }
        return JsonData.success();
    }

    @Override
    public Map findFocusTeacherByUserId(String userId, Integer page, Integer limit) {
        if (page != null && limit != null){
            PageHelper.startPage(page,limit,false);
        }
        List<TeacherVo> list = userConcernMapper.findFocusTeacherByUserId(userId);
        if (CollectionUtil.isNotEmpty(list)){
            for (TeacherVo teacherVo : list) {
                String userId1 = teacherVo.getUserId();
                Videos v = new Videos();
                v.setUserId(userId1);
                PageHelper.startPage(1,3,false);
                PageHelper.orderBy("my_recommend desc,create_time desc");
                List<Videos> select = videosMapper.select(v);
                teacherVo.setList(select);
            }
        }

        return JsonData.success(list);
    }
}
